package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsRegion;
import com.qf.service.AdminService;
import com.qf.service.CategoryService;
import com.qf.service.DtsBrandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private DtsBrandService brandService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public Object list(Integer id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc")String order){
        //1. 查询并返回分页对象
        IPage<DtsBrand> resultPage = brandService.queryBrandPage(id,name,page,limit,sort,order);

        //获取结果集
        List<DtsBrand> list = resultPage.getRecords();
        //获取查询总条数
        long total = resultPage.getTotal();

        //封装对象返回
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);

        return ResponseUtil.ok(data);

    }

    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){

        List<DtsAdmin> adminList = adminService.findAll();
        List<Map<String,Object>> list=new ArrayList<>(adminList.size());
        for (DtsAdmin admin : adminList) {
            Map<String,Object> datas=new HashMap<>(2);
            datas.put("value",admin.getId());
            datas.put("label",admin.getUsername());
            list.add(datas);
        }
        List<DtsCategory> l1CatList = categoryService.findByLabel();
        List<CatVo> categoryList = new ArrayList<>(l1CatList.size());

        for (DtsCategory l1 : l1CatList) {
            CatVo l1CatVo = new CatVo();
            l1CatVo.setValue(l1.getId());
            l1CatVo.setLabel(l1.getName());

            List<DtsCategory> l2CatList = categoryService.findByPid(l1.getId());
            List<CatVo> children = new ArrayList<>(l2CatList.size());
            for (DtsCategory l2 : l2CatList) {
                CatVo l2CatVo = new CatVo();
                l2CatVo.setValue(l2.getId());
                l2CatVo.setLabel(l2.getName());
                children.add(l2CatVo);
            }
            l1CatVo.setChildren(children);
            categoryList.add(l1CatVo);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("adminList",list);
        data.put("categoryList", categoryList);

        return ResponseUtil.ok(data);
    }

    /**
     * 品牌制造商添加
     * @return
     */
    @PostMapping("/create")
    public Object add(@RequestBody DtsBrand brand){
        int add = brandService.add(brand);

        if(add == 0){
            return  ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        int count = brandService.update(brand);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        Integer id = brand.getId();

        if(id==null){
            return ResponseUtil.badArgument();
        }

        brandService.deleteByid(id);

        return ResponseUtil.ok();

    }
}
